package cn.shuai.test;

import java.io.DataOutput;
import java.util.Iterator;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.type.EnumOrdinalTypeHandler;
import org.junit.Test;

import cn.shuai.dao.EmpDao;
import cn.shuai.dao.SqlSessionFactoryUtil;
import cn.shuai.entity.Dept;
import cn.shuai.entity.Emp;

public class Demo {
	@Test
	public void searchAll(){
		SqlSession se=null;
		try {
			se=SqlSessionFactoryUtil.getSqlSession();
			EmpDao dao=se.getMapper(EmpDao.class);
			List<Emp> list=dao.searchAll();
			
			for(Emp e:list){
				System.out.println(e.getEmpNo()+","+e.getEmpName()+","+e.getJob()+","+e.getSalary());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if (se!=null) {
				se.close();
			}
		}
	}
	
	@Test
	public void searchById(){
		SqlSession se=null;
		try {
			se=SqlSessionFactoryUtil.getSqlSession();
			EmpDao dao=se.getMapper(EmpDao.class);
			Emp e=dao.searchById(7499);
			System.out.println(e.getEmpNo()+","+e.getEmpName()+","+e.getJob()+","+e.getSalary());
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(se!=null){
				se.close();
			}
		}
	}
	
	@Test
	public void searchBySalAndDeptno(){
		SqlSession se=null;
		try {
			se=SqlSessionFactoryUtil.getSqlSession();
			EmpDao dao=se.getMapper(EmpDao.class);
			Emp emp=new Emp();
			emp.setSalary(2000);
			
			Dept dept=new Dept();
			dept.setDeptNo(20);
			emp.setDept(dept);
			
			List<Emp> list=dao.searchBySalAndDeptno(emp);
			for(Emp e:list){
				System.out.println(e.getEmpNo()+","+e.getEmpName()+","+e.getJob()+","+e.getSalary());
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(se!=null){
				se.close();
			}
		}
	}
	
	@Test
	public void searchEmpAndDept(){
		SqlSession se=null;
		try {
			se=SqlSessionFactoryUtil.getSqlSession();
			EmpDao dao=se.getMapper(EmpDao.class);
			Emp emp=new Emp();
			emp.setSalary(2000);
			
			Dept dept=new Dept();
			dept.setDeptNo(20);
			emp.setDept(dept);
			
			List<Emp> list=dao.searchEmpAndDept(emp);
			
			for (Emp e : list) {
				System.out.println(e.getEmpNo() + "," + e.getEmpName() + "," + e.getJob() + "," + e.getSalary() + ","
						+ e.getDept().getDeptName());
			}			
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(se!=null){
				se.close();
			}
		}
	}
	
	@Test
	public void addEmp(){
		SqlSession se=null;
		try {
			se=SqlSessionFactoryUtil.getSqlSession();
			EmpDao dao=se.getMapper(EmpDao.class);
			Emp emp=new Emp();
			emp.setEmpNo(8100);
			emp.setEmpName("关二爷");
			emp.setJob("耍大刀");
			emp.setSalary(1000);
			Dept dept=new Dept();
			dept.setDeptNo(10);
			emp.setDept(dept);
			
			int flag=dao.addEmp(emp);
			System.out.println("flag====="+flag);
			se.commit();
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(se!=null){
				se.close();
			}
		}
	}
	
	@Test
	public void updateEmp(){
		SqlSession se=null;
		
		try {
			se=SqlSessionFactoryUtil.getSqlSession();
			EmpDao dao=se.getMapper(EmpDao.class);
			Emp emp=dao.searchById(8100);
			emp.setEmpName("关公");
			
			Dept dept=new Dept();
			dept.setDeptNo(20);
			emp.setDept(dept);
			dao.updateEmp(emp);
			se.commit();
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(se!=null){
				se.close();
			}
		}
	}
}
